
*Import the building data: 
use "$root/Data/Original/bfs_sample_2019.dta", clear

frame create comm_class
frame change comm_class

use "$root/Data/Original/comm_categories.dta", clear

frame change default

frlink m:1 GDENR, frame(comm_class)

frget urban3, from(comm_class)

drop comm_class

tab GDENR if urban3==""


*Manually adjust the ones that were not matched, due to community mergers:
tab PLZ if GDENR==.

replace GDENR=371 if GDENR==. & PLZ=="2503"
replace GDENR=692 if GDENR==. & PLZ=="2743"

**Manually replace the GDENR from the mutations and do the link again: 
drop urban3 
replace GDENR=329 if GDENR==334
replace GDENR=409 if GDENR==416
replace GDENR=889 if GDENR==873
replace GDENR=889 if GDENR==874
replace GDENR=889 if GDENR==876
replace GDENR=879 if GDENR==881
replace GDENR=939 if GDENR==937
replace GDENR=981 if GDENR==996

frlink m:1 GDENR, frame(comm_class)

frget urban3, from(comm_class)

drop comm_class

destring urban3, force replace

*Change the one community that was manually looked up: 
replace urban3=3 if GDENR==661


**Add the PV data to the building dataset using first the coordinates and second the address: 

** Add the PV information from the dataset of all PVs: 
frame create PV
frame change PV

use "$root/Data/Original/powerplants.dta", clear

*We are only interested in PVs from canton Bern: 
keep if canton=="BE" & plant_cat2==3

*Drop if no geocoordinates in data:
drop if GKODX==. | GKODY==.


**Create an id variable to then collapse: 
egen id=group(GKODX GKODY)

*We do not want to merge PVs that were installed after the car purchase!
drop if year_install>2019


sort id year
collapse (firstnm) year_install plant_cat* PLZ  _x _y GKODX GKODY date_install (first) street municipality canton beginningofoperation adress (sum) initialpower totalpower detail_power2007 detail_power2009 detail_power2010 detail_power2011 detail_power2012 detail_power2013 detail_power2014 detail_power2015 detail_power2016 detail_power2017 detail_power2018 detail_power2019 detail_power2020 detail_power2021, by(id)


*check if now is id: 
isid GKODX GKODY

frame change default

frlink m:1 GKODX GKODY, frame(PV)
frget year_install total, from(PV)

*Create an additional PV frame and PV dataset to then use with merge: 
frame drop PV
frame create PV 
frame change PV

use "$root/Data/Original/powerplants.dta", clear

*We are only interested in PVs from canton Bern: 
keep if canton=="BE" & plant_cat2==3

egen id=group(adress)

drop if year_install>2019

sort id year
collapse (firstnm) year_install plant_cat* PLZ  _x _y GKODX GKODY date_install (first) street municipality canton beginningofoperation adress (sum) initialpower totalpower detail_power2007 detail_power2009 detail_power2010 detail_power2011 detail_power2012 detail_power2013 detail_power2014 detail_power2015 detail_power2016 detail_power2017 detail_power2018 detail_power2019 detail_power2020 detail_power2021, by(id)



save "$root/Data/Original/PV_bern_byadress.dta", replace

**match to building dataset: 
frame change default
rename Adress adress
merge m:1 adress using "$root/Data/Original/PV_bern_byadress.dta", keepusing(year_install totalpower) update
drop if _merge==2
drop _merge PV

*Generate the PVhh indicator: 
gen PVhh=(year_install!=.)


*Save the building information dataset: 
save "$root/Data/Produced/build_complete.dta", replace
